package gr.atc.epart;

import gr.atc.epart.actions.Action;
import gr.atc.epart.actions.Action.Type;
import gr.atc.epart.net.CreateFailedException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AddCommentActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.addcomment);
		Button createButton = (Button) findViewById(R.id.addcommentbutton);
		createButton.setOnClickListener(btnListerner);
	}

	private OnClickListener btnListerner = new OnClickListener() {
		public void onClick(View v) {

			if (v == findViewById(R.id.addcommentbutton)) {

				String username = AccountUtil.getAccountName(getBaseContext());
				Action selectedAction = EPartVolatileDataStore.getInstance()
						.getSelectedAction();
				int actionId = selectedAction.getId();

				TextView commentTextView = (TextView) findViewById(R.id.addcommenttext);
				String commentText = commentTextView.getText().toString();

				EPartClient client = new EPartClient();

				try {

					if (selectedAction.getType() == Type.ePetition) {

						client.voteForPetition(actionId, username, commentText);
						// TODO remove the commentOnAction method from here...
						if (!commentText.equals("")) {
							client.commentOnAction(actionId, username,
									commentText);
						}

					} else {

						client.commentOnAction(actionId, username, commentText);

					}

				} catch (CreateFailedException e) {
					Toast.makeText(getBaseContext(), "Creation Error",
							Toast.LENGTH_SHORT).show();

					setResult(200);

					finish();
				}

				Toast.makeText(getBaseContext(), "Added Successfully",
						Toast.LENGTH_SHORT).show();

				setResult(200);

				finish();

			}
		}

	};

}
